package org.yanex.project.api;

import java.util.ArrayList;
import java.util.List;

import org.yanex.project.api.HTTPMethods.HTTPMethodException;
import org.yanex.project.api.items.Category;
import org.yanex.project.api.items.City;
import org.yanex.project.api.items.Place;
import org.yanex.project.api.parsers.AbstractParser.ParserException;
import org.yanex.project.api.parsers.ParsedItem;
import org.yanex.project.api.parsers.XMLParser;

class APIXMLImpl implements API {

	@SuppressWarnings("unused")
	private String server;

	private final String ABOUT;
	private final String CONTACTS;
	private final String CITIES;
	private final String CATEGORIES;
	private final String PLACES;
	private final String PLACE;

	APIXMLImpl(String server) {
		if (!server.endsWith("/"))
			server=server+"/";
		
		this.server = server;

		ABOUT = server + "about";
		CONTACTS = server + "contacts";
		CITIES = server + "cities";
		CATEGORIES = server + "categories/";
		PLACES = server + "places/";
		PLACE = server + "place/";
	}

	public String getAbout() throws HTTPMethodException {
		return HTTPMethods.getInstance().get(ABOUT);
	}

	public String getContacts() throws HTTPMethodException {
		return HTTPMethods.getInstance().get(CONTACTS);
	}

	public List<City> getCities() throws HTTPMethodException, ParserException {
		String ret = HTTPMethods.getInstance().get(CITIES);
		XMLParser parser = new XMLParser();
		List<ParsedItem> items = parser.getItems(ret);
		
		List<City> cities = new ArrayList<City>();
		for (ParsedItem item : items) {
			try {
				City c = new City(item);
				cities.add(c);
			} catch (Exception e) {
				//wrong data, skip this item
			}
		}
		
		return cities;
	}

	public List<Category> getCategories(long cityId) 
			throws HTTPMethodException, ParserException {
		
		String url = CATEGORIES+"/"+cityId;
		String ret = HTTPMethods.getInstance().get(url);
		
		XMLParser parser = new XMLParser();
		List<ParsedItem> items = parser.getItems(ret);
		
		List<Category> categories = new ArrayList<Category>();
		for (ParsedItem item : items) {
			try {
				Category c = new Category(item);
				categories.add(c);
			} catch (Exception e) {
				//wrong data, skip this item
			}
		}
		
		return categories;
	}

	public List<Place> getPlaces(long categoryId) throws HTTPMethodException, ParserException {
		String url = PLACES+"/"+categoryId;
		String ret = HTTPMethods.getInstance().get(url);
		
		XMLParser parser = new XMLParser();
		List<ParsedItem> items = parser.getItems(ret);
		
		List<Place> places = new ArrayList<Place>();
		for (ParsedItem item : items) {
			try {
				Place p = new Place(item);
				places.add(p);
			} catch (Exception e) {
				//wrong data, skip this item
			}
		}
		
		return places;
	}

	public Place getPlace(long placeId) throws HTTPMethodException, ParserException {
		String url = PLACE+"/"+placeId;
		String ret = HTTPMethods.getInstance().get(url);
		
		XMLParser parser = new XMLParser();
		List<ParsedItem> items = parser.getItems(ret);
		
		for (ParsedItem item : items) {
			try {
				Place p = new Place(item);
				return p;
			} catch (Exception e) {
				//wrong data, skip this item
			}
		}
		
		return null;
	}

}
